﻿using Day22EF.Data;
using Day22EF.Data.Models;
using Day22EF.Models;
using System;
using System.Collections.Generic;
using System.Data.Entity.Validation;
using System.Linq;
using System.Web;

namespace Day22EF.Adapters.Data
{
    public class HomeDataAdapter : IHomeAdapter
    {
        public List<HeroViewModel> GetHeroListViewModels()
        {
            List<HeroViewModel> models = null;

            using (HeroDbContext db = new HeroDbContext())
            {
                models = db.Users.Select(u => new HeroViewModel
                {
                    AffiliationName = db.AffiliationUsers.FirstOrDefault(au => au.UserId == u.Id).Affiliation.Name,
                    HeroName = u.UserName,
                    PowerName = db.Powers.FirstOrDefault(p => p.UserId == u.Id).Name,
                    PowerType = db.Powers.FirstOrDefault(p => p.UserId == u.Id).PowerType.Name,
                    UserId = u.Id
                }).ToList();
            }

            return models;
        }

        public void CreateHero(CreateHeroViewModel model)
        {
            using (HeroDbContext db = new HeroDbContext())
            {
                try
                {
                    User dbUser = db.Users.Create();
                    dbUser.UserName = model.HeroName;

                    db.Users.Add(dbUser);
                    db.SaveChanges();

                    Power dbPower = db.Powers.Create();
                    dbPower.Name = model.PowerName;
                    dbPower.PowerTypeId = model.SelectedPowerTypeId;
                    dbPower.UserId = dbUser.Id;

                    db.Powers.Add(dbPower);

                    db.SaveChanges();
                }
                catch (DbEntityValidationException ex)
                {
                    foreach (var validationErrors in ex.EntityValidationErrors)
                    {
                        foreach (var validationError in validationErrors.ValidationErrors)
                        {
                            //Trace.TraceInformation("Property: {0} Error: {1}", validationError.PropertyName, validationError.ErrorMessage);
                        }
                    }
                }
            }
        }

        public void EditHero(EditHeroViewModel model)
        {
            using (HeroDbContext db = new HeroDbContext())
            {
                User dbUser = db.Users.Find(model.UserId);
                dbUser.UserName = model.HeroName;

                Power dbPower = db.Powers.Find(dbUser.Powers.FirstOrDefault().PowerId);
                dbPower.Name = model.PowerName;
                dbPower.PowerTypeId = model.SelectedPowerTypeId;

                db.SaveChanges();
            }
        }

        public CreateHeroViewModel GetCreateHeroViewModel()
        {
            CreateHeroViewModel model = new CreateHeroViewModel();

            using(HeroDbContext db = new HeroDbContext())
            {
                model.PowerTypes = db.PowerTypes.Select(p =>
                    new PowerTypeViewModel()
                    {
                        PowerTypeId = p.PowerTypeId,
                        Name = p.Name
                    }).ToList();
            }

            return model;
        }

        public HeroViewModel GetHeroViewModel(string id)
        {
            HeroViewModel model = null;

            using (HeroDbContext db = new HeroDbContext())
            {
                model = db.Users.Where(u => u.Id == id).Select(u => new HeroViewModel
                {
                    AffiliationName = db.AffiliationUsers.FirstOrDefault(au => au.UserId == u.Id).Affiliation.Name,
                    HeroName = u.UserName,
                    PowerName = db.Powers.FirstOrDefault(p => p.UserId == u.Id).Name,
                    PowerType = db.Powers.FirstOrDefault(p => p.UserId == u.Id).PowerType.Name,
                    UserId = u.Id
                }).FirstOrDefault();
            }

            return model;
        }

        public EditHeroViewModel GetEditHeroViewModel(string id)
        {
            EditHeroViewModel model = null;

            using (HeroDbContext db = new HeroDbContext())
            {
                model = db.Users.Where(u => u.Id == id).Select(u => new EditHeroViewModel
                {
                    HeroName = u.UserName,
                    PowerName = db.Powers.FirstOrDefault(p => p.UserId == u.Id).Name,
                    PowerTypes = db.PowerTypes.Select(pt => new PowerTypeViewModel
                    {
                        Name = pt.Name,
                        PowerTypeId = pt.PowerTypeId
                    }).ToList(),
                    SelectedPowerTypeId = db.Powers.FirstOrDefault(p => p.UserId == u.Id).PowerTypeId,
                    UserId = u.Id
                }).FirstOrDefault();
            }

            return model;
        }
    }
}